//
//  YXTool.m
//  MockProject
//
//  Created by FanYu on 29/8/2016.
//  Copyright © 2016 niuzhaowang. All rights reserved.
//

#import "YXTool.h"
#import "AFNetworking.h"


@implementation YXTool

+ (NSString *)subString:(NSString *)str from:(NSInteger)index {
    return [str substringFromIndex:index];
}

+ (NSString *)currentDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    NSDate *currentDate = [NSDate date];
    
    return [dateFormatter stringFromDate:currentDate];
}

+ (NSString *)sizeStringForBytes:(unsigned long long)aBytes {
    // 小于1KB
    if (aBytes < 1024) {
        return @"< 1K";
    }
    // 1KB - 1MB
    if ((aBytes >= 1024) && (aBytes < 1024*1024)) {
        float kb = (float)aBytes / 1024.f;
        return [NSString stringWithFormat:@"%.2fK", kb];
    }
    // 1MB - 1GB
    if ((aBytes >= 1024*1024) && (aBytes < 1024*1024*1024)) {
        float mb = (float)aBytes / (1024.f*1024.f);
        return [NSString stringWithFormat:@"%.2fM", mb];
    }
    // 大于等于1GB
    if (aBytes >= 1024*1024*1024) {
        float gb = (float)aBytes / (1024.f*1024.f*1024.f);
        return [NSString stringWithFormat:@"%.2fG", gb];
    }
    return @"";
}

+ (NSString *)spaceSizeStringForBytes:(unsigned long long)aBytes {
    if (aBytes < 1024ull*1024*1024) {
        float mb = (float)aBytes / (1024.f*1024.f);
        if (mb < 0.1)
            return [NSString stringWithFormat:@"%.0fM", mb];
        else
            return [NSString stringWithFormat:@"%.1fM", mb];
    } else {
        float gb = (float)aBytes / (1024.f*1024.f*1024.f);
        return [NSString stringWithFormat:@"%.1fG", gb];
    }
}

@end
